/* Filename: timelog.c
 * Description: 每秒钟往/tmp/out写入一行当前时间
 * Last modified: humble 20200316 21:25
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define FNAME "/tmp/out"
#define BUFSIZE (1024)

static void usage(void);

int main(int argc, char **argv) {
    if(argc != 1){ fprintf(stderr, "argc !=1"); usage(); exit(1); }

    FILE *fp = fopen(FNAME, "a+");
    if(!fp){ perror("fopen"); exit(1); }

    int count = 0;
    char buf[BUFSIZE] = {0};
    while(fgets(buf, BUFSIZE, fp)){ count++; }

    time_t stamp = 0;
    struct tm *tmptr = NULL;
    while(1){
        stamp = time(0);
        tmptr = localtime(&stamp);
        //strftime(buf, BUFSIZE, "<TODO>", tmptr)
        fprintf(fp, "%4d %d-%d-%d %d:%d:%d\n", ++count, tmptr->tm_year + 1900, tmptr->tm_mon + 1, tmptr->tm_mday, tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec);
        fflush(fp);
        sleep(1);
    }

    fclose(fp);
    return 0;
}

static void usage(void) {
#define USAGE "Usage:\n\
  Helloworld\n"
    printf(USAGE);
}
